package org.Image.management.ViewService.Enroll;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.Image.management.SpringDataService.Pojo.Result.Result;
import org.Image.management.SpringDataService.Service.Sys_User.Pojo.dto.RegisterRequest;
import org.Image.management.SpringDataService.Service.Sys_User.controller.UserController;
import org.Image.management.ViewCofig.file.FxmlPath;
import org.Image.management.ViewCofig.file.IconPath;
import org.Image.management.ViewController.MainViewApp;
import org.Image.management.ViewService.utils.ByPieceToolMethod;
import org.Image.management.ViewService.utils.informationalTips;
import org.springframework.stereotype.Controller;

import java.util.Objects;

import static org.Image.management.ViewService.utils.informationalTips.setPrompt;


@Slf4j
@Controller
@RequiredArgsConstructor
public class EnrollController {
    //依赖注入 用户服务类
    private final UserController userController;

    @FXML
    public Label prompt;

    @FXML
    public TextField Field_name;

    @FXML
    public PasswordField Field_password1;

    @FXML
    public PasswordField Field_password2;

    @FXML
    public CheckBox choose_displayPassword;

    @FXML
    public TextField display_password;

    @FXML
    public TextField display_password1;

    @FXML
    public BorderPane setBackground;

    @FXML
    private void initialize() {

    }

    //创建账户
    @FXML
    void createAnAccount(ActionEvent event) {
        String account = getAccount();
        String password = getPassword();

        if (Objects.isNull(password) || Objects.isNull(account)) {
            return;
        }
        // 创建账户  需要链接数据库
        Result<?> register = userController.register(new RegisterRequest(account, password));

        if (!register.isSuccess()) {
            log.error("创建账户失败:{}", register);
            setPrompt(prompt, register.getMessage());
            return;
        }

        MainViewApp.setResizable.changeView(FxmlPath.Login, true, false);
    }

    //显示密码
    @FXML
    void displayPassword(ActionEvent event) {
        boolean selected = choose_displayPassword.isSelected();
        // 批量更改按键的 “setVisible”（是否隐藏）
        ByPieceToolMethod.batchSetVisible(selected, display_password, display_password1);
        ByPieceToolMethod.batchSetVisible(!selected, Field_password1, Field_password2);

        if (selected) {
            display_password.setText(Field_password1.getText());
            display_password1.setText(Field_password2.getText());
            return;
        }

        Field_password1.setText(display_password.getText());
        Field_password2.setText(display_password1.getText());
    }

    //去登录
    @FXML
    void gotoLogin(ActionEvent event) {
        MainViewApp.setResizable.changeView(FxmlPath.Login, true, false);
    }

    //清除文本
    @FXML
    void purgeText(ActionEvent event) {
        Field_name.setText("");
        Field_password1.setText("");
        Field_password2.setText("");
        display_password.setText("");
        display_password1.setText("");
    }


    //获取账户
    private String getAccount() {
        /*
         * @NotBlank(message = "用户名不能为空且不能只包含空格")
         * @Size(min = 2, max = 20, message = "用户名长度需在2-20字符之间")
         * */
        String text = Field_name.getText();
        if (text.isEmpty() || text.matches("\\s+")) {
            informationalTips.setPrompt(prompt, "请输入用户名并不能只包含空格");
            return null;
        }

        if (!text.matches(".{2,20}")) {
            informationalTips.setPrompt(prompt, "用户名长度需在2-20字符之间");
            return null;
        }
        return text;
    }

    //获取密码
    private String getPassword() {
        String text, text1;
        if (choose_displayPassword.isSelected()) {
            text = display_password.getText();
            text1 = display_password1.getText();
        } else {
            text = Field_password1.getText();
            text1 = Field_password2.getText();
        }

        /*
         * @Pattern(regexp = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$",
         *        message = "密码必须至少8位，包含字母和数字")
         * @Size(min = 8, max = 20, message = "密码长度需在8-20字符之间")
         * */
        if (text.isEmpty() || text1.isEmpty()) {
            setPrompt(prompt, "密码为空");
            return null;
        }
        if (!text.matches(".{8,20}") || !text1.matches(".{8,20}")) {
            setPrompt(prompt, "密码长度需在8-20字符之间");
            return null;
        }
        if (!text.matches("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$") || !text1.matches("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$")) {
            setPrompt(prompt, "密码必须至少8位，包含字母和数字");
            return null;
        }
        if (!text.equals(text1)) {
            setPrompt(prompt, "两次密码输入不正确");
            return null;
        }
        return text;
    }
    @FXML
    public void AccountCallback(ActionEvent actionEvent) {
        //  账号找回
        MainViewApp.setResizable.ModifyAccountBack((value) -> {
            Result<?> result = userController.deleteUser(value);
            if (!result.isSuccess()) {
                informationalTips.AlertDialogBox(Alert.AlertType.ERROR, IconPath.CRY_FILLING.getIconURL(), "", result.getMessage(), "账号找回 失败");
                return;
            }
            informationalTips.AlertDialogBox(Alert.AlertType.INFORMATION, IconPath.EMAIL.getIconURL(), "", value.getName()+"成功找回", "账号找回成功");

        });
    }
}
